<?php
//
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$this->model('member')->checklogin();
$this->model('member')->up_tixianfee();
if($_RQ['op']=='main'){
	//注册电商客会员
	$member=pdo_get('jy_newganfee_member',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
	$member['jiqiendtime1']=date('Y-m-d',$member['jiqiendtime']);
	$fu_feetype=$this->fenxiaoset['fu_feetype']?:1;
	$item=model('core/member')->detail($_SESSION['uid'],array('credit'.$fu_feetype,'credit2','credit3','mobile','avatar','nickname'));
	$item=array_merge($item,$member);
	$item['fu_fee']=$item['credit'.$fu_feetype];
	$item['fu_feetitle']=$this->gift_type[$fu_feetype];
	$item['id']=$_SESSION['uid'];
	$item['mycoupon']=pdo_count('jy_weishop_coupon_getlog',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'islock'=>0,'isuse'=>0));
	$item['level']=pdo_getcolumn('jy_weishop_member',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),'level');
	$item['level'] =pdo_getcolumn('jy_weishop_member_level',array('id'=>$item['level'],'uniacid'=>$_SESSION['uniacid']),'title');
	$item['level'] = $item['level']?$item['level']:'普通会员';
	$return['setting']=$return['pluginset'];
	$my_days = array(
				array('title'=>'我的优惠券','icon'=>'icon-youhuiquan','color'=>'#cc99ff','action'=>'jy_weishop/coupon.myindex/'),
				array('title'=>'微信会员卡','icon'=>'icon-daifahuo','color'=>'#cc99ff','action'=>'core/member.wxcard/main/'),
				//array('title'=>'余额提现','icon'=>'icon-dixiaofei','color'=>'#cc99ff','action'=>'core/member.tixian/'),
				array('title'=>'地址管理','icon'=>'icon-position','color'=>'#cc99ff','action'=>'core/member.address/'),
				array('title'=>'帮助中心','icon'=>'icon-help','color'=>'#cc99ff','action'=>'jy_weishop/news.list/'),
				//array('title'=>'获得购物卡','icon'=>'icon-qiangdan','color'=>'#cc99ff','action'=>'jy_weishop/member.getcard/'),
				array('title'=>'我的购物车','icon'=>'icon-gouwuche','color'=>'#cc99ff','action'=>'jy_weishop/member.cart/'),
				//array('title'=>'我的购物卡','icon'=>'icon-pingzhengzhongxin','color'=>'#cc99ff','action'=>'jy_weishop/member.mycard/'),
				//array('title'=>'我的关注','icon'=>'icon-woguanzhudepinpai','color'=>'#cc99ff','action'=>'jy_weishop/member.favorite/'),
				//array('title'=>'我的足迹','icon'=>'icon-zuji','color'=>'#cc99ff','action'=>'jy_weishop/member.history/'),
	);
	$return['setting']['my_days'] = !empty($return['setting']['my_days']) ? $return['setting']['my_days'] : $my_days;
	$my_logs = array(
				array('title'=>'积分日志','icon'=>'icon-jifen','color'=>'#cc99ff','action'=>'core/member.credit_log/'),
				array('title'=>'余额日志','icon'=>'icon-jiangjiatongzhi','color'=>'#cc99ff','action'=>'core/member.credit_log/'),
				array('title'=>'提现日志','icon'=>'icon-trade','color'=>'#cc99ff','action'=>'core/member.cash_log/'),
				array('title'=>'红包日志','icon'=>'icon-ai-hongbao','color'=>'#cc99ff','action'=>'core/member.red_log/'),
	);
	$return['setting']['my_logs'] = !empty($return['setting']['my_logs']) ? $return['setting']['my_logs'] : $my_logs;
	$my_memberbtn = array(
				array('title'=>'保证金日志','icon'=>'icon-jifen','color'=>'#cc99ff','action'=>'jy_newganfee/msglog//type=5'),
				array('title'=>'领取优惠券','icon'=>'icon-youhuiquan1','color'=>'#cc99ff','action'=>'jy_weishop/coupon.index/'),
				//array('title'=>'领取微信会员卡','icon'=>'icon-huiyuanqia','color'=>'#cc99ff','action'=>'core/member.wxcard/main'),
				array('title'=>'积分排行','icon'=>'icon-paixing','color'=>'#cc99ff','action'=>'core/member.rank/'),
	);
	$install = check_account_permission('jy_hehuofen');
	if($return['pluginset']['ishehuo'] && $install && $_SESSION['current_plugin'] !='jy_friends_shop'){
		$fenxiaoset=plugin('jy_hehuofen')->plugin_setting('page_setting');
		$my_memberbtn[]=array('action'=>'jy_hehuofen/index/','title'=>'合伙人入口','icon'=>'icon-paihao0','remark'=>'');
	}
	$return['setting']['my_memberbtn'] = !empty($return['setting']['my_memberbtn']) ? $return['setting']['my_memberbtn'] : $my_memberbtn;
	$return['item']=$item;
	$status0 = pdo_count('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'status'=>0));
	$status1 = pdo_count('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'status'=>1));
	$status2 = pdo_count('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'status'=>2));
	$status3 = pdo_count('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'status'=>3));

	$wxcard_setting=uni_setting('wx_card_setting');
	$wxcard_edit=uni_setting('wx_card_edit');

	if($wxcard_edit['card_id'] && $wxcard_edit['status']==1 && $wxcard_setting['open']){
		//存在会员卡且允许领取微信会员卡，检查是否已领卡
		$check=pdo_getcolumn('core_fans',array('openid'=>$_SESSION['openid'],'uniacid'=>$_SESSION['uniacid'],'wxgid'=>$_SESSION['wechat_id']),'wxcard_code');
		if(!$check){
			$wxcard=1;
		}
	}
	if(!$wxcard){
		array_splice($menu_list,3,1);
	}
	//判断配送员
	$install = check_account_permission('jy_peisong');
	if($install){
		$return['is_peisong'] = pdo_getcolumn('jy_peisong_member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']),'id');
	}
	//判断店员
	$install = check_account_permission('jy_peisong');
	if($install){
		$return['is_worker'] = pdo_getcolumn('jy_weishop_store_worker',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']),'id');
	}
	/*$return['htmls']['list'][]=array('type'=>'menu_list','list'=>$menu_list);
	$return['htmls']['list'][]=array('type'=>'submit_list','list'=>array(
		array('url'=>'core/member.cpassword/','title'=>'修改密码'),
		array('url'=>'core/member.logout/','title'=>'退出登陆','title_color'=>'#fff','background'=>'red'),
	));*/
	$sale_luck=plugin('jy_weishop')->plugin_setting('sale_luck');
	if($sale_luck['isopen']==1){
		$return['sale_luck']=model('jy_weishop/activity/sales')->getluck(true);
	}
	$return['closedsales']=1;
	$return['setpay']=uni_setting('pay');
	$return['menu_list']=$menu_list;
	$return['feemoney']=plugin('jy_weishop')->plugin_setting('feemoney');
	if($return['feemoney']['isopen']==1){
		//节省数据
		$daytime = mktime(0,0,0,date('m'),date('d'),date('Y'));
		$monthtime=mktime(0,0,0,date('m'),1,date('Y'));
		$yearstime=strtotime(date("Y",time())."-1"."-1");
		$lastyearstime=strtotime(date("Y",time())."-2"."-1");
		$daysgoods=pdo_getall('jy_weishop_order_goods',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'createtime >'=>$daytime));
		$return['feemoney_day'] = 0;
		foreach($daysgoods as $dv){
					$status = pdo_getcolumn('jy_weishop_order',array('id'=>$dv['orderid'],'uniacid'=>$_SESSION['uniacid']),'status');
					if($status!=1 && $status!=2 && $status!=3){
						continue;
					}
					$goodsinfo = pdo_get('jy_weishop_goods',array('id'=>$dv['goodsid'],'uniacid'=>$_SESSION['uniacid']),array('id','marketprice'));
					$dv['feemoney']=$goodsinfo['marketprice']>0?$goodsinfo['marketprice']-$dv['price']:$return['feemoney']['feerade']*$dv['price']/100;
					$dv['feemoney']=$dv['total']*$dv['feemoney'];
					$return['feemoney_day'] +=$dv['feemoney'];
		}
		$return['feemoney_day']=price($return['feemoney_day']);
		
		$monthgoods=pdo_getall('jy_weishop_order_goods',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'createtime >'=>$monthtime));
		$return['feemoney_month'] = 0;
		foreach($monthgoods as $dv){
					$status = pdo_getcolumn('jy_weishop_order',array('id'=>$dv['orderid'],'uniacid'=>$_SESSION['uniacid']),'status');
					if($status!=1 && $status!=2 && $status!=3){
						continue;
					}
					$goodsinfo = pdo_get('jy_weishop_goods',array('id'=>$dv['goodsid'],'uniacid'=>$_SESSION['uniacid']),array('id','marketprice'));
					$dv['feemoney']=$goodsinfo['marketprice']>0?$goodsinfo['marketprice']-$dv['price']:$return['feemoney']['feerade']*$dv['price']/100;
					$dv['feemoney']=$dv['total']*$dv['feemoney'];
					$return['feemoney_month'] +=$dv['feemoney'];
		}
		$return['feemoney_month']=price($return['feemoney_month']);		
		$yearsgoods=pdo_getall('jy_weishop_order_goods',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'createtime >'=>$yearstime));
		$return['feemoney_years'] = 0;
		foreach($yearsgoods as $dv){
					$status = pdo_getcolumn('jy_weishop_order',array('id'=>$dv['orderid'],'uniacid'=>$_SESSION['uniacid']),'status');
					if($status!=1 && $status!=2 && $status!=3){
						continue;
					}
					$goodsinfo = pdo_get('jy_weishop_goods',array('id'=>$dv['goodsid'],'uniacid'=>$_SESSION['uniacid']),array('id','marketprice'));
					$dv['feemoney']=$goodsinfo['marketprice']>0?$goodsinfo['marketprice']-$dv['price']:$return['feemoney']['feerade']*$dv['price']/100;
					$dv['feemoney']=$dv['total']*$dv['feemoney'];
					$return['feemoney_years'] +=$dv['feemoney'];
		}
		$return['feemoney_years']=price($return['feemoney_years']);
		
		$lastyearsgoods=pdo_getall('jy_weishop_order_goods',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'createtime between'=>array($yearstime,$lastyearstime)));
		$return['feemoney_lastyears'] = 0;
		foreach($lastyearsgoods as $dv){
					$status = pdo_getcolumn('jy_weishop_order',array('id'=>$dv['orderid'],'uniacid'=>$_SESSION['uniacid']),'status');
					if($status!=1 && $status!=2 && $status!=3){
						continue;
					}
					$goodsinfo = pdo_get('jy_weishop_goods',array('id'=>$dv['goodsid'],'uniacid'=>$_SESSION['uniacid']),array('id','marketprice'));
					$dv['feemoney']=$goodsinfo['marketprice']>0?$goodsinfo['marketprice']-$dv['price']:$return['feemoney']['feerade']*$dv['price']/100;
					$dv['feemoney']=$dv['total']*$dv['feemoney'];
					$return['feemoney_lastyears'] +=$dv['feemoney'];
		}
		$return['feemoney_lastyears']=price($return['feemoney_lastyears']);
	}
	$current_plugin=$_SESSION['current_plugin']?:'jy_weishoppei';
	$credit_types=credit_types();
	$return['credit3title']=$credit_types[3];
	set_html('title','会员中心');
	exi($return);
}
//购买机器人
if($_RQ['op']=='buyjiqi'){
	set_html('title','购买机器人');
	$basic=$this->plugin_setting('basic');
	$post=array();
	$post['num']=1;
	$post['checked']=true;
	$return['jiqixieyi']=$this->plugin_setting('jiqixieyi');
	if($_RQ['postdata']){
		$postdata=get_postdata();
		$packnum=$postdata['num'];
		$fee=$basic['jiqi_fee']*$packnum;
		//创建一个会员订单
		$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$_SESSION['uid'],
			'num'=>$basic['jiqi_buynian']?$packnum:0,
			'ordersn'=>get_ordersn('jy_newganfee_buyjiqi','ordersn'),
			'fee'=>$fee,
			'ispay'=>0,
			'status'=>0,
			'createtime'=>TIMESTAMP,
		);
		pdo_insert('jy_newganfee_buyjiqi',$insert);
		$id=pdo_insertid();
		$params['fee']=$fee;
		$params['title']='购买机器人订单';
		$params['id']=$id;
		$params['plugin']='jy_newganfee';
		$params['pay_type']='wechat';//可选，此项作用为指定支付方式
		$params['no_type']='credit';//可选，此项作用为指定不允许使用哪种支付方式
		$params['back_url']='jy_newganfee/myhome/payresult/';
		$return['payid']=cfc('pay')->create_pay_order($params);
	}
	$return['post']=$post;
	$return['fee']=price($basic['jiqi_fee']);
	$return['vipbg']=$basic['vipbg'];
	$return['jiqi_buynian']=$basic['jiqi_buynian'];
	exi($return);
}

if($_RQ['op']=='payresult'){
	$payresult=$_SESSION['pay_result'];
	if(!$payresult){
		exi('支付失败，未收到支付结果！','error');
	}
	if($payresult['status']!=1){
		exi('支付失败！','error');
	}
	$order=pdo_get('jy_newganfee_buyjiqi',array('id'=>$payresult['id'],'uniacid'=>$_SESSION['uniacid']));
	if(!$order){
		exi('支付失败！购买机器人订单不存在！','error');
	}
	if($order['fee']!=$payresult['fee']){
		exi('支付失败！购买机器人订单金额不匹配！','error');
	}
	pdo_update('jy_newganfee_buyjiqi',array('status'=>1,'ispay'=>1),array('id'=>$order['id']));
	//计算到期时间
	$startTime =time();
	$member=pdo_get('jy_newganfee_member',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
	if($member['jiqiendtime']!=0){
		$addTime=$member['jiqiendtime']+$order['num']*365*24*3600;
	}else{
		$addTime =$startTime+$order['num']*365*24*3600;
	}
	$addTime=$order['num']?$addTime:0;
	pdo_update('jy_newganfee_member',array('jsjiqi'=>1,'jiqiendtime'=>$addTime),array('mid'=>$order['mid'],'uniacid'=>$_SESSION['uniacid']));
	//发送模板消息
	$this->notice('buyjiqi',$_SESSION['uid'],'',$order['id']);
	exi('支付成功','success','jy_newganfee/myhome/main/');
}
if($_RQ['op']=='tixian'){
	$post=get_postdata();
	if(!$post['fee']){
		exi('请输入要提现的额度！','error');
	}
	$member=pdo_get('jy_newganfee_member',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
	if($member['baofee']<$post['fee']){
		exi('提现的额度不能大于当前总额！','error');
	}else{
		$this->model('member')->recharge($_SESSION['uid'],$post['fee'],6,'提现保证金');
		cfc('core')->member_credit($_SESSION['uid'],'credit2',$post['fee'],1,'jy_newganfee','提现保证金');
		exi('操作成功！！');
	}
}
?>